package org.zhu.LamadaInterface;

import org.zhu.LamadaInterface.myclass.Person;
import org.zhu.LamadaInterface.myclass.SomeTime;
import org.zhu.LamadaInterface.myinteface.MyFunctionInterFace;
import org.zhu.LamadaInterface.myinteface.PersonFactory;

import java.util.function.Consumer;

/**
 * @author yingzhi zhu
 * date 2017/12/7.
 */
public class Client1 {
    public static void main(String[] args) {
//        functionInterfaceTest();
//        etaTest();
        lamadaLimit();
    }

    /**
     * 函数试接口测试
     */
    private static void functionInterfaceTest() {
        MyFunctionInterFace<Integer, Integer> functionalInterface = (x) -> x + x + x + x + x;
        System.out.println("Lamada表达式相加" + functionalInterface.addition(3));
        System.out.println("默认方法相乘" + functionalInterface.multiplication(3));

    }

    static int num1 = 3;

    /**
     * 对Lamada函数 变量作用域进行测试 lamada表达数如同匿名类一样 可改变方法外的变量的值 但不能改变方法内的值 所有对方法类的变量必须为常量 jvm因该是吧他识别为常量。
     * 试接口测试
     */
    private static void lamadaLimit() {
        int num = 2;
        MyFunctionInterFace<Integer, Integer> functionalInterface = (x) -> {
            return (x + num);
        };

        MyFunctionInterFace<Integer, Integer> functionalInterface1 = (x) -> {
            num1 = num1 + 3;
            return (x + num1);
        };


        System.out.println("1Lamada表达式相加" + functionalInterface.addition(3));
        System.out.println("2Lamada表达式相加" + functionalInterface1.addition(3));

    }

    /**
     * Lamada ::方法测试
     */
    private static void etaTest() {
        out(System.out::println);
        PersonFactory<Person> personFactory = Person::new;
        Person person = personFactory.creatPerson("Peter", "Parker");
        System.out.println(person.getFirstName());

        SomeTime someTime = new SomeTime();
        MyFunctionInterFace<Integer, String> functionalInterface1 = Integer::valueOf;
        MyFunctionInterFace<String, String> functionalInterface2 = someTime::doSomeThing;
        System.out.println("Lamada表达式::简写 静态方法测试：" + functionalInterface1.addition("13123"));
        System.out.println("Lamada表达式::简写 非静态方法测试：" + functionalInterface2.addition("heheda"));
    }


    private static void out(Consumer<String> function) {
        function.accept("asdasd");
    }


}
